package com.lungle.warmdiary.app.view;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.lungle.warmdiary.R;
import com.lungle.warmdiary.app.AppConstant;
import com.lungle.warmdiary.app.AppContext;
import com.lungle.warmdiary.app.AppManager;
import com.lungle.warmdiary.app.api.remote.ApiFactory;
import com.lungle.warmdiary.app.base.BaseSubscriber;
import com.lungle.warmdiary.app.model.Result;
import com.lungle.warmdiary.app.model.response.User;
import com.lungle.warmdiary.app.utils.SystemBarTintManager;
import com.umeng.analytics.MobclickAgent;

import cn.jpush.android.api.JPushInterface;
import rx.schedulers.Schedulers;


public class AppStart extends Activity {
    private static final String APP_START_SCREEN = "SplashScreen";
    public static final int MAX_WATTING_TIME = 1000;// 停留时间3秒


    @Override
    public void onCreate(Bundle savedInstanceState) {
        Activity activity = AppManager.getActivity(MainActivity.class);
        if (activity != null && !activity.isFinishing()) {
            finish();
        }

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);

        View root = findViewById(R.id.app_start_view);
        if (root != null && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP
                && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setStatusBarTintResource(R.color.transparent);
        }

        new Handler().postDelayed(() -> redirectTo(), MAX_WATTING_TIME);
        excuteDefaultTask();
    }


    @Override
    public void onResume() {
        super.onResume();
        JPushInterface.onResume(this);
        MobclickAgent.onPageStart(APP_START_SCREEN);
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        JPushInterface.onPause(this);
        MobclickAgent.onPageEnd(APP_START_SCREEN);
        MobclickAgent.onPause(this);
    }

    @TargetApi(19)
    protected void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }


    private void redirectTo() {
//        if (AppContext.getInstance().isLogin()) {
            Intent intent = new Intent(this, MainActivity.class);
            if (getIntent().getBundleExtra(AppConstant.EXTRA_BUNDLE) != null) {
                intent.putExtra(MainActivity.ACTION_KEY, MainActivity.ACTION_REDIRECT_MESSAGE);

            } else {

                intent.putExtra(MainActivity.ACTION_KEY, MainActivity.ACTION_REDIRECT_DEFALULT);
            }
            startActivity(intent);
//        } else {
//            Intent intent = new Intent(this, LoginActivity.class);
//            startActivity(intent);
//
//        }

        finish();

    }


    private void excuteDefaultTask() {
        if (AppContext.getInstance().isLogin())
            checkUserInfo();

    }


    private void checkUserInfo() {
        ApiFactory.getUserApi().getUserProfile().subscribeOn(Schedulers.io())
                .observeOn(Schedulers.io()).subscribe(new BaseSubscriber<Result<User>>(this) {
            @Override
            public void onNext(Result<User> userResult) {
//                if (userResult.getData() != null) {
                    AppContext.getInstance().saveLoginInfo(userResult.getData());
//                }else{
//                    AppContext.getInstance().cleanLoginInfo();
//                    Intent intent = new Intent(AppStart.this, LoginActivity.class);
//                    startActivity(intent);
//                }

            }
        }).unsubscribe();
    }
}